package com.boot.springboot.component.qlexpress.service;


import com.boot.springboot.common.utils.AESUtil;
import com.boot.springboot.common.utils.Base64Util;
import com.boot.springboot.common.utils.RSAUtil;

public class DecryptHelper {

    private static final String TAG = "DecryptHelper";

    private static final String RSA_PRIVATE_KEY = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAsPgXMva94yw7q7QT\n" +
            "a/66H+XsGnvKFKL5oqY9ZI/fGrZvMQgRZQXBwaGI2RUme28rL+jJO2fd1CtMEPGU\n" +
            "Yr1sIQIDAQABAkB7XZceie85jD5UpgXcBQjO78M1exIWbL5Faknl7n2C6n2rWD0E\n" +
            "MNL2oTEvTc6AnyCL/eRzxwIDgA72MJA8x1AhAiEA2TLF33+Bn/SZdjuGro+hgX5f\n" +
            "R6fKKFUDkI0kX6V7mtUCIQDQlX8lvBAFj/9pKfCyxC1/fj2TogI/X22Pb8Qo3RZa\n" +
            "HQIgIYhLMFQ5KP7UaoKKKo0WPC3Me/2VomBtqnV/9hxQVvkCIQCKRaBdLndciKHj\n" +
            "YCctCrmym8hMNwjQY/1oKcpa4H0xzQIgdQlqDFqu1SXbeyJ1f4t9RYArij7rbrQt\n" +
            "jEh1VUSGAg0=";

    public static String decrypt(String content, String encryptAesKey) {

        if (content == null || content.isEmpty()) {
            return null;
        }

        if (encryptAesKey == null || encryptAesKey.isEmpty()) {
            return null;
        }

        byte[] decryptAesKey = null;
        try {
            decryptAesKey = RSAUtil.decryptWithPrivateKey(Base64Util.decode(encryptAesKey), Base64Util.decodeNoWrap(RSA_PRIVATE_KEY));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(TAG + "decrypt: decryptAesKey = " + decryptAesKey);

        String decryptData = null;
        try {
            decryptData = AESUtil.decrypt(content, decryptAesKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(TAG +  "decrypt: decryptData = " + decryptData);

        return decryptData;
    }
}
